
import {
  USER_LOGIN_SUCCESS,
  LOGIN_DEFEATED,
} from '../actions/constants'
import { cloneDeep } from '../utils/tools'

// 初始化reducer
const initState = {
  userInfo: null,
  token: null,
}

// 定义reducer
const reducer = (state = initState, {type, payload}) => {
  // 深拷贝state
  let copy = cloneDeep(state)

  switch (type) {
  case USER_LOGIN_SUCCESS : // ！登录成功
    copy.userInfo = payload
    copy.token = payload.token
    return copy

  case LOGIN_DEFEATED: // ！登录失败，重置
    // copy.userInfo = null
    copy.token = null
    return copy

  default:
    return state
  }
}

export default reducer

